package com.voiceqsologger.controller;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.voiceqsologger.entity.AsrTask;
import com.voiceqsologger.service.AsrService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * ASR 控制器：提供上传识别与查询结果接口
 */
@RestController
@RequestMapping("/api/asr")
@Validated
@RequiredArgsConstructor
public class AsrController {

    private final AsrService asrService;

    @Value("${storage.uploadDir}")
    private String uploadDir;

    /**
     * 上传音频并发起识别
     */
    @PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Map<String, Object> recognize(@RequestPart("file") @NotNull MultipartFile file,
                                         @RequestParam(value = "mode", required = false, defaultValue = "local") String mode) {
        // mode: local(默认，本地 Whisper) | api(云端接口)
        String taskId = asrService.createAndStart(file, uploadDir, mode);
        return Map.of("taskId", taskId, "mode", mode);
    }

    /**
     * 查询任务结果
     */
    @GetMapping("/task/{id}")
    public AsrTask get(@PathVariable("id") String id) {
        return Db.getById(id, AsrTask.class);
    }
}


